﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

using System.Threading;
using System.Windows.Forms;

namespace Interchanger
{
    public partial class Read : Form
    {
        public Read()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        string cache = "";
        bool Pause = false;

        List<string> Words = new List<string>();
        private void button1_Click(object sender, EventArgs e)
        {

           
        }

        private void Read_Load(object sender, EventArgs e)
        {

            CPU("loadcache");
        label4.Top=   textBox1.Top = ClientRectangle.Top + (ClientRectangle.Height / 2);
         label4.Left=  textBox1.Left = ClientRectangle.Top + (ClientRectangle.Width / 2);
            trackBar1.Height = ClientRectangle.Height;
            trackBar1.Top = ClientRectangle.Top;
            //   trackBar1.Value = textBox1.Top;
            trackBar1.Maximum = ClientRectangle.Height - textBox1.Height;
            panel2.Height = ClientRectangle.Height;
            panel2.Top = ClientRectangle.Top;
            panel2.SendToBack();
            panel5.Top = ClientRectangle.Top;
          panel5.Left = ClientRectangle.Left;
            panel5.Width = panel1.Width;
            panel1.Top = ClientRectangle.Top;
            panel5.SendToBack();

            trackBar2.Left = ClientRectangle.Left;
            trackBar2.Width = ClientRectangle.Width;
            trackBar2.Top = ClientRectangle.Bottom - trackBar2.Height;

            panel8.Top = ClientRectangle.Bottom - panel8.Height;

        }

        void CPU(string action)
        {
            Task Anti_Freeze = new Task(() =>
            {try
                    {
                Task LoadProperties = new Task(() =>
                {
                    

                        switch (action)
                        {
                            case "read/start":
                                while (Pause == false)
                                {
                                    if (checkBox2.Checked == false)
                                    {
                                        label4.Hide();
                                        textBox1.Show();
                                        foreach (string Reading in Words)
                                        {
                                            if (checkBox3.Checked == true)
                                            {
                                                textBox1.Left = (this.Width / 2) - (textBox1.Width / 2);
                                            }
                                            if (checkBox2.Checked == true)
                                            { CPU("read/start"); Thread.CurrentThread.Abort(); }
                                            Size textSize = TextRenderer.MeasureText(Reading.Replace(" ", ""), textBox1.Font);
                                            if (textSize.Width + 5 > textBox1.Width)
                                            {
                                                textBox1.Width = textSize.Width + 8;

                                            } if (textSize.Height + 5 > textBox1.Height)
                                            {
                                                textBox1.Height = textSize.Height + 5;

                                            }

                                            if (checkBox1.Checked)
                                            {
                                                if (textBox1.Width - 10 > textSize.Width)
                                                {
                                                    textBox1.Width = textSize.Width + 5;
                                                }
                                                if (textBox1.Height - 10 > textSize.Height)
                                                {
                                                    textBox1.Height = textSize.Height + 5;
                                                }
                                            }


                                            textBox1.Text = Reading;
                                            Thread.Sleep(trackBar2.Value );
                                            if (Pause == true  )
                                            {
                                                Thread.CurrentThread.Abort();
                                            }
                                           
                                        }
                                    }
                                    else if (checkBox2.Checked ==true )
                                    {
                                        textBox1.Hide();
                                        label4.Show();

                                        //continue
                                        foreach (string Reading in Words)
                                        {
                                            try
                                            {
                                                label4.Text = Reading;
                                                Thread.Sleep(trackBar2.Value);
                                                if (checkBox3.Checked == true)
                                                {
                                                    label4.Left = (this.Width / 2) - (label4.Width / 2);
                                                }

                                                if (Pause == true)
                                                {
                                                    Thread.CurrentThread.Abort();
                                                }
                                                if (checkBox2.Checked == false)
                                                { CPU("read/start"); Thread.CurrentThread.Abort(); }
                                            }
                                            catch (Exception) { Thread.CurrentThread.Abort(); }
                                        }

                                    }
                                }

                                break;

                            case "loadcache":
                                StreamReader Cashing = new StreamReader(Properties.Settings.Default.File);
                                {
                                    Task FWait = new Task(() =>
                                    {


                                        cache = Cashing.ReadToEnd();
                                    }); FWait.Start(); FWait.Wait();

                                    string[] WLords = cache.Split(' ');

                                    foreach (string lol in WLords)
                                    {
                                        Words.Add(lol);
                                    }
                                    Cashing.Close();
                                }
                                break;


                            case "close": //Done
                                Properties.Settings.Default.Action = false;
                                Properties.Settings.Default.File = "";
                                this.Close();
                                break;
                        }




                   

                });
                LoadProperties.Start();
                LoadProperties.Wait();

                    }
            catch (Exception)
            {





            }

            }); Anti_Freeze.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
       label4.Top=     textBox1.Top = trackBar1.Maximum - trackBar1.Value;
            

        }

        private void panel2_MouseEnter(object sender, EventArgs e)
        {
            trackBar1.Show();
        }

        private void trackBar1_MouseLeave(object sender, EventArgs e)
        {
            trackBar1.Hide();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {

        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel4_MouseClick(object sender, MouseEventArgs e)
        {
            DialogResult Cvalue = colorDialog1.ShowDialog();

            if (Cvalue.ToString().ToLowerInvariant() == "ok")
            {

                this.BackColor = colorDialog1.Color;
            }
            //  MessageBox.Show(Cvalue.ToString());
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {


        }

        private void panel3_MouseClick(object sender, MouseEventArgs e)
        {
            DialogResult Cvalue = colorDialog1.ShowDialog();

            if (Cvalue.ToString().ToLowerInvariant() == "ok")
            {

                label4.ForeColor = colorDialog1.Color;
                textBox1.ForeColor = colorDialog1.Color;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            try
            {
                float Value = int.Parse(comboBox1.SelectedItem.ToString());
                Thread.Sleep(25);
             textBox1.Font = new Font(textBox1.Font.FontFamily, Value);
             label4.Font = new Font(textBox1.Font.FontFamily, Value);
                
            }
            catch (Exception) { }
        }

        private void panel1_MouseLeave(object sender, EventArgs e)
        {
            if (close == false)
            {

                close = true;
            }
            else
            {
                panel1.Hide();
            }
            
        }

        private void panel5_MouseEnter(object sender, EventArgs e)
        {
            panel1.Show();
        }

        private void panel6_MouseClick(object sender, MouseEventArgs e)
        {
            DialogResult Cvalue = colorDialog1.ShowDialog();

            if (Cvalue.ToString().ToLowerInvariant() == "ok")
            {

                label4.BackColor = textBox1.BackColor = colorDialog1.Color;
            }
         
        }
        bool close = true;
        private void checkBox1_MouseEnter(object sender, EventArgs e)
        {
            close = false;
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label1_MouseEnter(object sender, EventArgs e)
        {
            close = false;
        }

        private void panel3_MouseEnter(object sender, EventArgs e)
        {
            close = false;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            Pause = true;
            CPU("close");
            this.Close();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Pause = false;
            CPU("read/start");
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            Pause = true;

        }

        private void panel8_MouseEnter(object sender, EventArgs e)
        {
            trackBar2.Show();
        }

        private void trackBar2_MouseLeave(object sender, EventArgs e)
        {
            trackBar2.Hide();
        }


    }
}
